from django.http import JsonResponse


class HttpCode:
    ok = 200
    paramsError = 400
    unauth = 401
    serverError = 500


def result(code=HttpCode.ok, message="", data=None, **kwargs):
    json_dict = {"code": code, "message": message, "data": data}

    if kwargs and isinstance(kwargs, dict) and kwargs.keys():
        json_dict.update(kwargs)

    return JsonResponse(json_dict)


def ok(message="成功", data=None):
    return result(code=HttpCode.ok, message=message, data=data)


def params_error(message="参数错误", data=None):
    return result(code=HttpCode.paramsError, message=message, data=data)


def unauth(message="未经授权", data=None):
    return result(code=HttpCode.unauth, message=message, data=data)


def server_error(message="服务器错误", data=None):
    return result(code=HttpCode.serverError, message=message, data=data)
